package com.example.googlemapdrawroutelast;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.android.gms.maps.model.LatLng;


public class CoordinatesParser {
	
    static ArrayList<LatLng> coordinatesLatLng;
    
	public static String getStringFromCoordinates(List<LatLng> coordinatesToParse)  throws Exception
	{
	 String coordinates;
		
		 coordinates = "{" +  coordinatesToParse.size() + "}";
		 
			for(int i = 0; i < coordinatesToParse.size(); i++)
			{	
				coordinates += getStringFromCoordinate(coordinatesToParse.get(i));
			}
			
			return coordinates;		
	}

	public static String getStringFromCoordinate(LatLng coordinate) 
	{
		return "[" + coordinate.latitude + "," + coordinate.longitude + "]";
	}
	
	public static ArrayList<LatLng> getCoordinatesFromString(String a_sCoordinates) throws Exception
	{		
		float lat;
		float lon = 0;
	      String pattern = "\\d+";	      
	      // Create a Pattern object
	      Pattern r = Pattern.compile(pattern);

	      // Now create matcher object.
	      Matcher m = r.matcher(a_sCoordinates);


	      if (m.find( )) {
	         System.out.println("Found value: " + m.group() );
	         int arrLength = Integer.parseInt(m.group().toString());	      
		    
		      coordinatesLatLng = new ArrayList<LatLng>();
	
		      pattern = "[-+]?[0-9]*\\.[0-9]+";
		      Pattern pat = Pattern.compile(pattern);
		      Matcher matcher = pat.matcher(a_sCoordinates);
		      int iterator = 0;
		      while (matcher.find()) {
		    	  
		    	  System.out.println(matcher.group());

		    	  lat = Float.parseFloat(matcher.group().toString());
		    	  
		    	  if(matcher.find())
		    	  {
		    		  System.out.println(matcher.group());
		    		 lon = Float.parseFloat(matcher.group().toString());
		    	  }
		    	  
		    	   coordinatesLatLng.add(iterator,new LatLng(lat, lon));
		    	  iterator++;
		      }
	      }
	      
	      return coordinatesLatLng;
	}
}